import 'package:flutter/material.dart';
import 'package:common/global/style.dart';
import 'package:common/common.dart';

class EnvDialog extends StatefulWidget {
  static show(
    BuildContext context,
  ) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return Dialog(
            child: EnvDialog(),
            clipBehavior: Clip.antiAlias,
          );
        });
  }

  @override
  State<StatefulWidget> createState() => _EnvDialogState();
}

class _EnvDialogState extends State<EnvDialog> {
  var _controller = TextEditingController();
  String env = "";
  BuildType buildType;

  @override
  void initState() {
    super.initState();
    Environment.getEnv().then((value) {
      setState(() {
        env = value;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10),
      height: 240,
      width: 287,
      child: Column(children: [
        Expanded(
            child: Container(
          alignment: Alignment.center,
          padding: EdgeInsets.all(5),
          child: Text(
            "切换环境后需杀掉App并重启生效",
            style: TextStyle(fontSize: 14, color: Colors.red),
          ),
        )),
        Expanded(
            child: GestureDetector(
          onTap: () {
            setState(() {
              env = Environment.Test;
              buildType = BuildType.Test;
            });
          },
          child: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            color: Environment.Test == env ? Colors.green : Colors.white,
            child: Text(
              "Test 环境",
              style: TextStyle(fontSize: 14, color: Colors.black),
            ),
          ),
        )),
        Expanded(
            child: GestureDetector(
          onTap: () {
            setState(() {
              env = Environment.Pre;
              buildType = BuildType.Pre;
            });
          },
          child: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            color: Environment.Pre == env ? Colors.green : Colors.white,
            child: Text(
              "Pre 环境",
              style: TextStyle(fontSize: 14, color: Colors.black),
            ),
          ),
        )),
        Expanded(
            child: GestureDetector(
          onTap: () {
            setState(() {
              env = Environment.Release;
              buildType = BuildType.Release;
            });
          },
          child: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            color: Environment.Release == env ? Colors.green : Colors.white,
            child: Text(
              "Release 环境",
              style: TextStyle(fontSize: 14, color: Colors.black),
            ),
          ),
        )),
        Expanded(
          child: Container(
            decoration: BoxDecoration(border: Border.all(color: Colors.blueGrey)),
            alignment: Alignment.center,
            margin: EdgeInsets.only(top: 5),
            child: TextField(
              style: TextStyle(
                fontSize: 14,
                color: Colors.black,
              ),
              controller: _controller,
              decoration: InputDecoration(
                hintText: "请输入密码",
                border: InputBorder.none,
                isCollapsed: true,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ),
        Container(
            padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buttonWidget('取消', () {
                  Navigator.pop(context);
                }, isSure: false),
                _buttonWidget('确认', () {
                  if (buildType != null) {
                    ///pd@2021
                    if ("pd@2021" == _controller.text) {
                      Environment.setEnv(buildType);
                      Navigator.pop(context);
                    } else {
                      Fluttertoast.showToast(msg: "密码不正确");
                    }
                  } else {
                    Fluttertoast.showToast(msg: "无需切换");
                  }
                }, isSure: true),
              ],
            ))
      ]),
    );
  }

  Widget _buttonWidget(String title, Function onTop, {bool isSure = true}) {
    return FlatButton(
      child: Container(
        child: Text(
          title,
          style: TextStyle(color: isSure ? Colors.white : TColor.primeColor, fontSize: 15),
        ),
      ),
      onPressed: onTop,
      color: isSure ? TColor.primeColor : TColor.primeColor.withOpacity(0.4),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    );
  }
}
